package com.github.caijh.mall.front.controller;


import javax.servlet.http.HttpServletRequest;

import com.github.caijh.framework.web.controller.BaseController;
import com.github.caijh.framework.web.util.RequestUtils;
import com.github.caijh.mall.common.CommonResult;
import com.github.caijh.mall.constants.Constants;
import com.github.caijh.mall.front.request.LoginMobileRequest;
import com.github.caijh.mall.front.request.LoginRequest;
import com.github.caijh.mall.front.request.RegisterRequest;
import com.github.caijh.mall.front.response.LoginRespBody;
import com.github.caijh.mall.sms.service.SmsService;
import com.github.caijh.mall.user.service.UserService;
import com.github.caijh.mall.utils.ValidateFormUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController("FrontLoginController")
@RequestMapping("api/front")
@Api(tags = "用户 -- 登录注册")
public class LoginController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private SmsService smsService;

    @ApiOperation(value = "手机号登录接口")
    @PostMapping(value = "/login/mobile")
    public CommonResult<LoginRespBody> phoneLogin(@RequestBody @Validated LoginMobileRequest loginRequest, HttpServletRequest request) throws Exception {
        String clientIp = RequestUtils.getIp(request);
        RegisterRequest registerRequest = new RegisterRequest();
        BeanUtils.copyProperties(loginRequest, registerRequest);
        return CommonResult.success(userService.register(registerRequest, clientIp));
    }

    @ApiOperation(value = "账号密码登录")
    @PostMapping(value = "/login")
    public CommonResult<LoginRespBody> login(@RequestBody @Validated LoginRequest loginRequest) throws Exception {
        return CommonResult.success(userService.login(loginRequest));
    }


    @ApiOperation(value = "退出")
    @GetMapping(value = "/logout")
    public CommonResult<String> loginOut(HttpServletRequest request) {
        userService.loginOut(RequestUtils.extractHeader(request, Constants.HEADER_AUTHORIZATION_KEY));
        return CommonResult.success();
    }

    /**
     * 发送短信
     *
     * @param phone 手机号码
     * @return 发送是否成功
     */
    @ApiOperation(value = " 发送短信")
    @PostMapping(value = "/sendCode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataTypeClass = String.class)
    })
    public CommonResult<Object> sendCode(@RequestParam String phone) {
        ValidateFormUtil.isPhone(phone, "手机号码错误");
        final Boolean ret = smsService.pushCodeToList(phone, 1, null);
        if (Boolean.TRUE.equals(ret)) {
            return CommonResult.success("发送成功");
        } else {
            return CommonResult.failed("发送失败");
        }
    }

}



